// import com.alibaba.druid.sql.ast.SQLExpr;
// import com.alibaba.druid.sql.ast.SQLSubPartitionBy;
// import com.alibaba.druid.sql.ast.statement.SQLColumnDefinition;
// import com.alibaba.druid.sql.dialect.mysql.ast.MySqlObject;
// import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitor;
// import com.alibaba.druid.sql.visitor.SQLASTVisitor;

// import java.util.ArrayList;
// import java.util.std::list;

#include "MySqlSubPartitionByList.h"
#include "../../../../../sql/visitor/SQLASTVisitor.h"
#include "../../../../../sql/dialect/mysql/visitor/MySqlASTVisitor.h"
#include "../../../../../sql/ast/SQLExpr.h"
#include "../../../../../sql/ast/statement/SQLColumnDefinition.h"
#include "../../../../../utils/instanceof.h"
#include "../../../../../Exception/IllegalArgumentException.h"
#include "../../../../../utils/listTran.h"
#include "../../../../../utils/log.h"

MySqlSubPartitionByList::MySqlSubPartitionByList()
{
  keys = std::make_shared<std::list<SQLExpr_ptr>>();
  columns = std::make_shared<std::list<SQLColumnDefinition_ptr>>();
}

// @Override
void MySqlSubPartitionByList::accept0(SQLASTVisitor_ptr visitor)
{
  if (instanceof <SQLASTVisitor, MySqlASTVisitor>(visitor.get()))
  {
    MySqlASTVisitor_ptr tmp = std::dynamic_pointer_cast<MySqlASTVisitor>(visitor);
    accept0(tmp);
  }
  else
  {
    throw new IllegalArgumentException(make_string_ptr("not support visitor type : " + *visitor->getClass()));
  }
}

// @Override
void MySqlSubPartitionByList::accept0(MySqlASTVisitor_ptr visitor)
{
  if (visitor->visit(SharedObject(MySqlSubPartitionByList)))
  {
    SQLObject_list_ptr  tmp0 = ListTran::tran_class<SQLExpr, SQLObject>(keys);
    acceptChild(visitor, tmp0);
    SQLObject_list_ptr  tmp1 = ListTran::tran_class<SQLColumnDefinition, SQLObject>(columns);
    acceptChild(visitor, tmp1);
    acceptChild(visitor, subPartitionsCount);
  }
  visitor->endVisit(SharedObject(MySqlSubPartitionByList));
}

SQLExpr_list_ptr  MySqlSubPartitionByList::getKeys()
{
  return keys;
}

void MySqlSubPartitionByList::addKey(SQLExpr_ptr key)
{
  if (key != nullptr)
  {
    key->setParent(SharedObject(MySqlSubPartitionByList));
  }
  this->keys->push_back(key);
}

SQLColumnDefinition_list_ptr  MySqlSubPartitionByList::getColumns()
{
  return columns;
}

void MySqlSubPartitionByList::addColumn(SQLColumnDefinition_ptr column)
{
  if (column != nullptr)
  {
    column->setParent(SharedObject(MySqlSubPartitionByList));
  }
  this->columns->push_back(column);
}

void MySqlSubPartitionByList::cloneTo(MySqlSubPartitionByList_ptr x)
{
  SQLSubPartitionBy::cloneTo(x);
  for (SQLExpr_ptr key : *keys)
  {
    SQLExpr_ptr k2 = std::dynamic_pointer_cast<SQLExpr>(key->clone());
    k2->setParent(x);
    x->keys->push_back(k2);
  }
  for (SQLColumnDefinition_ptr column : *columns)
  {
    SQLColumnDefinition_ptr c2 = std::dynamic_pointer_cast<SQLColumnDefinition>(column->clone());
    c2->setParent(x);
    x->columns->push_back(c2);
  }
}

SQLObject_ptr MySqlSubPartitionByList::clone()
{
  SQLObject_ptr clone_tmp = std::shared_ptr<MySqlSubPartitionByList>(new MySqlSubPartitionByList());
  std::shared_ptr<MySqlSubPartitionByList> x = std::dynamic_pointer_cast<MySqlSubPartitionByList>(clone_tmp);
  // MySqlSubPartitionByList *x = new MySqlSubPartitionByList();
  cloneTo(x);
  return x;
}
